package me.firstapp.service.shop.impl;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.MD5Utils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.PointRechargeCard;
import me.firstapp.module.system.CommonSequence;
import me.firstapp.repository.shop.PointRechargeCardRepository;
import me.firstapp.repository.system.CommonSequenceRepository;
import me.firstapp.service.shop.PointRechargeCardService;

@Service
public class PointRechargeCardServiceImpl implements PointRechargeCardService {

	@Autowired
	private PointRechargeCardRepository pointRechargeCardRepository;

	@Autowired
	private CommonSequenceRepository commonSequenceRepository;

	@Override
	public Page<PointRechargeCard> findPointRechargeCardPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		return pointRechargeCardRepository.findPointRechargeCardPage(pageNo, pageSize, searchParams, sortParam,
				sortType);
	}

	@Override
	public void addPointRechargeCard(Double pointValue, Double price, Integer counts) {
		if (pointValue == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "积分值");
		}
		if (price == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "价格");
		}
		if (counts == null) {
			counts = 1;
		}
		if (counts != null && counts <= 0) {
			counts = 1;
		}
		for (int i = 0; i < counts; i++) {
			PointRechargeCard card = new PointRechargeCard();
			CommonSequence commonSequence = new CommonSequence();
			commonSequenceRepository.save(commonSequence);
			card.setCardNo(createCardNo(commonSequence.getId()));
			card.setCreateTime(new Date());
			String password = UUID.randomUUID().toString().replaceAll("-", "");
			card.setPassword(password);
			card.setPointValue(pointValue);
			card.setSalt(MD5Utils.generate(pointValue.toString()));
			card.setPrice(price);
			card.setStatus(0);
			card.setUseStatus(0);
			pointRechargeCardRepository.save(card);
		}
	}

	private String createCardNo(Long sequenceNumber) {
		String prefix = "100";
		String suffix = String.format("%1$010d", sequenceNumber);
		String orderNo = prefix + suffix;
		return orderNo;
	}

	@Override
	public void executeStartUse(String ids, Long startDate, Long endDate) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "卡id");
		}
		if (startDate == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "起始日期");
		}
		if (endDate == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "结束日期");
		}
		Date _startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startDate), "yyyy-MM-dd");
		Date _endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endDate), "yyyy-MM-dd");
		String[] idsArr = ids.split(",");
		for (int i = 0; i < idsArr.length; i++) {
			String idStr = idsArr[i];
			if (StrUtils.isNotNULL(idStr)) {
				Long id = Long.valueOf(idStr);
				PointRechargeCard card = (PointRechargeCard) pointRechargeCardRepository.getById(id);
				if (card != null) {
					if (card.getStatus() != 0 && card.getUseStatus() != 0) {
						throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_CAN_NOT_START_USE);
					}
					card.setStatus(1);
					card.setStartDate(_startDate);
					card.setEndDate(_endDate);
					pointRechargeCardRepository.update(card);
				}
			}
		}
	}

	@Override
	public void executeStartSell(Long id) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "卡id");
		}
		PointRechargeCard card = (PointRechargeCard) pointRechargeCardRepository.getById(id);
		if (card == null) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_NOT_EXIST);
		}
		if (card.getUseStatus() != 0 && card.getStatus() != 1) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_CAN_NOT_START_SELL);
		}
		card.setUseStatus(1);
		pointRechargeCardRepository.update(card);
	}

	@Override
	public void executeStopUse(Long id) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "卡id");
		}
		PointRechargeCard card = (PointRechargeCard) pointRechargeCardRepository.getById(id);
		if (card == null) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_NOT_EXIST);
		}
		if (card.getUseStatus() != 0 && card.getStatus() != 1) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_CAN_NOT_STOP_USE);
		}
		card.setStatus(0);
		card.setStartDate(null);
		card.setEndDate(null);
		pointRechargeCardRepository.update(card);
	}

	@Override
	public void executeSelled(Long id) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "卡id");
		}
		PointRechargeCard card = (PointRechargeCard) pointRechargeCardRepository.getById(id);
		if (card == null) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_NOT_EXIST);
		}
		if (card.getUseStatus() != 1 && card.getStatus() != 1) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_CAN_NOT_SELLED);
		}
		card.setUseStatus(2);
		pointRechargeCardRepository.update(card);
	}
}
